package com.jy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.pojo.CheckItem;
import com.jy.service.CheckItemService;
import com.jy.utils.MessageConstant;
import com.jy.utils.QueryPageBean;
import com.jy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("checkitem")
public class CheckItemController {
    @Autowired
    CheckItemService checkItemService;

    @RequestMapping("add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            return Result.error(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return Result.ok(MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findPage")
    public Page<CheckItem> findPage(@RequestBody QueryPageBean qb) {
        return checkItemService.findPage(qb);
    }

    @RequestMapping("delete")
    public Result delete(Integer id) {
        try {
            checkItemService.delete(id);
        } catch (Exception e) {
            return Result.error(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return Result.ok(MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findById")
    public Result findById(Integer id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return Result.ok(checkItem);
        } catch (Exception e) {
            return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            return Result.error(MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return Result.ok(MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("findAll")
    public Result findAl() {
        try {
            List<CheckItem> list =checkItemService.findAll();
            return Result.ok(list);
        } catch (Exception e) {
            return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
